﻿using Digitalmes.Core.Domain.Eitites;
using Digitalmes.Core.Domain.Repositories;

namespace Digitalmes.Core.Services.Impl;

/// <summary>
/// 转盘实时数据服务
/// </summary>
/// <param name="repo"></param>
internal sealed class TurntableRealtimeService(IRepository<TurntableRealtimeRecord> repo, ICounterService counterService) : ITurntableRealtimeService
{
    public async Task<(List<TurntableRealtimeRecord>, int)> GetPageListAsync(DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize, CancellationToken cancellationToken = default)
    {
        return await repo.AsQueryable()
            .WhereIF(startTime.HasValue, m => m.ArchiveTime >= startTime.MinTime())
            .WhereIF(endTime.HasValue, m => m.ArchiveTime <= endTime.MaxTime())
            .ToPagationAsync(pageIndex, pageSize, cancellationToken).ConfigureAwait(false);
    }

    public async Task<List<TurntableRealtimeRecord>> GetListAsync(DateTime? startTime, DateTime? endTime, CancellationToken cancellationToken = default)
    {
        return await repo.AsQueryable()
            .WhereIF(startTime.HasValue, m => m.ArchiveTime >= startTime.MinTime())
            .WhereIF(endTime.HasValue, m => m.ArchiveTime <= endTime.MaxTime())
            .ToListAsync(cancellationToken).ConfigureAwait(false);
    }

    public async Task InsertAsync(TurntableRealtimeRecord input, CancellationToken cancellationToken = default)
    {
        int currentCount = await counterService.GetCurrentAsync("number_rotations").ConfigureAwait(false) + 1;
        input.Number = currentCount;
        await repo.InsertAsync(input, cancellationToken).ConfigureAwait(false);
    }
}
